 /*******************************************************************************
  * 文件：DevError.c
  * 作者：djy
  * 版本：v1.0.0
  * 日期：2020-11-27
  * 说明: 故障模式
*******************************************************************************/
#include "DevError.h"
#include "StateMachine.h"
#include "DisplayBoard.h"
#include "OS_Timer.h"
#include "DevInfor.h"
#include "FireCtrl.h"
#include "BuzCtrl.h"
#include "TimingCtrl.h"
#include "main.h"

/* 宏定义 *********************************************************************/
/* 类型定义 *******************************************************************/
/* 变量定义 *******************************************************************/
static ErrorCtrl_ts sErrorCtrl;
/* 函数声明 *******************************************************************/
/* 函数定义 *******************************************************************/
static void ErrorFlashHook_500MS(void)
{
    if(eDEV_ERROR != StateMachine_GetCurState())
    {
        // 不在故障模式，提前退出
        OS_TimerStop(&sErrorCtrl.sFlashTimer);
        return;
    }

    if(TRUE == Dev_CheckDemoModeFlag())
    {
        // 已在演示模式，提前退出
        DevErrorExit();
        return;
    }
    
    if(0 == OS_TimerGetExpiredTimes(&sErrorCtrl.sFlashTimer)%2)
    {
        // 关闭数码管
        SMG_AllOff();
    }
    else
    {
        // 数码管显示故障码
        switch(Dev_GetErrorType())
        {
        case eERR_SENSOR:
            SMG_ShowSymbol(eE4);
        break;
        case eERR_FXQ:
            SMG_ShowSymbol(eE5);
        break;
        case eERR_VOLTAGE:
            SMG_ShowSymbol(eE6);
        break;
        case eERR_FLAMEOUT:
            // SMG_ShowSymbol(eE2);
        break;
        case eERR_FIRE_FAIL:
            SMG_ShowSymbol(eE1);
        break;
        default:
        break;
        }
    }

    // 闪烁时间到
    switch(Dev_GetErrorType())
    {
    case eERR_SENSOR:
        if(40 == OS_TimerGetExpiredTimes(&sErrorCtrl.sFlashTimer))
        {
            DevErrorExit();
        }
    break;
    case eERR_VOLTAGE:
        if(20 == OS_TimerGetExpiredTimes(&sErrorCtrl.sFlashTimer))
        {
             DevErrorExit();
        }
    break;
    case eERR_FLAMEOUT:
        if(20 == OS_TimerGetExpiredTimes(&sErrorCtrl.sFlashTimer))
        {
            DevErrorExit();
        }
    break;
    case eERR_FIRE_FAIL:
        if(40 == OS_TimerGetExpiredTimes(&sErrorCtrl.sFlashTimer))
        {
            DevErrorExit();
        }
    break;
    default:
    break;
    }
}

U32 DevError0(U32 arg)
{
    // 关闭+/-指示灯
    LED_AddCtrl(eLED_OFF);
    LED_SubCtrl(eLED_OFF);
    SMG_IconMinute(OFF);
    SMG_IconTemper(OFF);
    
    // 根据故障位置，控制炉头指示灯，并关火
    switch(Dev_GetErrorPos())
    {
    case eERR_LEFT:
        // 左灶关火
        LeftStoveFireOff();
        // 停止左灶定时
        Timing_StopLeftTiming();
        // 左灶指示灯全亮
        LED_LeftTimingCtrl(eLED_ON);
        // 其他灶头切换至后台
        if(ON == Timing_CheckMidTimingRun())
        {
            LED_MidTimingCtrl(eLED_HALF);
        }
        else
        {
            LED_MidTimingCtrl(eLED_OFF);
        }
        if(ON == Timing_CheckRightTimingRun())
        {
            LED_RightTimingCtrl(eLED_HALF);
        }
        else
        {
            LED_RightTimingCtrl(eLED_OFF);
        }
    break;
    case eERR_MID:
        // 关闭中灶火
        MidStoveFireOff();
        // 停止中灶定时
        Timing_StopMidTiming();
        // 中灶指示灯全亮
        LED_MidTimingCtrl(eLED_ON);
        // 其他灶头指示灯切换至后台
        if(ON == Timing_CheckLeftTimingRun())
        {
            LED_LeftTimingCtrl(eLED_HALF);
        }
        else
        {
            LED_LeftTimingCtrl(eLED_OFF);
        }
        if(ON == Timing_CheckRightTimingRun())
        {
            LED_RightTimingCtrl(eLED_HALF);
        }
        else
        {
            LED_RightTimingCtrl(eLED_OFF);
        }
    break;
    case eERR_RIGHT:
        // 关闭右灶火
        RightStoveFireOff();
        // 停止右灶定时
        Timing_StopRightTiming();
        // 右灶指示灯常亮
        LED_RightTimingCtrl(eLED_ON);
        // 其他灶头进入后台
        if(ON == Timing_CheckLeftTimingRun())
        {
            LED_LeftTimingCtrl(eLED_HALF);
        }
        else
        {
            LED_LeftTimingCtrl(eLED_OFF);
        }
        if(ON == Timing_CheckMidTimingRun())
        {
            LED_MidTimingCtrl(eLED_HALF);
        }
        else
        {
            LED_MidTimingCtrl(eLED_OFF);
        }
    break;
    case eERR_ALL:
        LED_LeftTimingCtrl(eLED_OFF);
        LED_MidTimingCtrl(eLED_OFF);
        LED_RightTimingCtrl(eLED_OFF);
        LeftStoveFireOff();
        MidStoveFireOff();
        RightStoveFireOff();
        Timing_StopLeftTiming();
        Timing_StopMidTiming();
        Timing_StopRightTiming();
    break;
    }

    // 根据故障类型，报警
    switch(Dev_GetErrorType())
    {
    case eERR_DRY_FIRE:
        BuzCtrl(eBUZ_DRY_FIRE);
    break;
    case eERR_SENSOR:
        BuzCtrl(eBUZ_FXQ_ERR);
        SMG_ShowSymbol(eE4);
        OS_TimerRestart(&sErrorCtrl.sFlashTimer,500,ErrorFlashHook_500MS);
    break;
    case eERR_FXQ:
        BuzCtrl(eBUZ_FXQ_ERR);
        SMG_ShowSymbol(eE5);
        OS_TimerRestart(&sErrorCtrl.sFlashTimer,500,ErrorFlashHook_500MS);
    break;
    case eERR_VOLTAGE:
        BuzCtrl(eBUZ_VOLTAGE_ERR);
        SMG_ShowSymbol(eE6);
        OS_TimerRestart(&sErrorCtrl.sFlashTimer,500,ErrorFlashHook_500MS);
    break;
    case eERR_FLAMEOUT:
        // BuzCtrl(eBUZ_FLAMEOUT);
        // SMG_ShowSymbol(eE2);
        OS_TimerRestart(&sErrorCtrl.sFlashTimer,500,ErrorFlashHook_500MS);
    break;
    case eERR_FIRE_FAIL:
        BuzCtrl(eBUZ_FIRE_FAIL);
        SMG_ShowSymbol(eE1);
        OS_TimerRestart(&sErrorCtrl.sFlashTimer,500,ErrorFlashHook_500MS);
    break;
    default:
    break;
    }
	return	STATE_CTRL_INIT;
}

U32 DevError1(U32 arg)
{
  	return	STATE_CTRL_WAIT;
}

void DevErrorExit(void)
{
    // 清除故障标志
    // sErrorCtrl.eErrorPos  = 0;
    // sErrorCtrl.eErrorType = eERR_NONE;

    // 停止蜂鸣器
 	BuzStop();

    // 根据故障位置关闭相应LED
    switch (sErrorCtrl.eErrorPos)
    {
    case eERR_LEFT:
        LED_LeftTimingCtrl(eLED_OFF);
    break;
    case eERR_MID:
        LED_MidTimingCtrl(eLED_OFF);
    break;
    case eERR_RIGHT:
        LED_RightTimingCtrl(eLED_OFF);
    break;
    default:
    break;
    }
 	
    // 停止故障报警定时器
    OS_TimerStop(&sErrorCtrl.sFlashTimer);

    // 状态切换
    if(eDEV_ERROR == StateMachine_GetCurState())
    {
        if(ON == Timing_CheckLeftTimingRun())
        {
            // 切换到左灶定时
            StateMachine_Change(eDEV_LEFT_TIMING);
        }
        else if(ON == Timing_CheckMidTimingRun())
        {
            // 切换到中灶定时
            StateMachine_Change(eDEV_MID_TIMING);
        }
        else if(ON == Timing_CheckRightTimingRun())
        {
            // 切换到右灶定时
            StateMachine_Change(eDEV_RIGHT_TIMING);
        }
        else 
        {
            // 切换到待机状态
            StateMachine_Change(eDEV_STANDBY);
        }
    }
}

void Dev_SetErrorType(ErrorPos_te eErrPos,ErrorType_te eErrorType)
{
    sErrorCtrl.eErrorPos  = eErrPos;
    sErrorCtrl.eErrorType = eErrorType;
}

ErrorType_te Dev_GetErrorType(void)
{
    return sErrorCtrl.eErrorType;
}

ErrorPos_te  Dev_GetErrorPos(void)
{
    return sErrorCtrl.eErrorPos;
}

